<?php

include("pznahar/config.inc.php");
#include(CORE_PATH."gameconfig.inc.php");
include(CORE_PATH."log.php");

include(CORE_PATH."DBConnect.class.php");
include(CORE_PATH."staticclass/connect.php");
include(CORE_PATH."staticclass/habars.php");
include(CORE_PATH."staticclass/pet.php");
include(CORE_PATH."staticclass/book.php");
include(CORE_PATH."staticclass/level.php");
include(CORE_PATH."staticclass/shop.php");
include(CORE_PATH."staticclass/cvest.php");
#include("pznahar/staticclass/artifact.php");
#include("pznahar/staticclass/boiler.php");
#include("pznahar/staticclass/mortar.php");
#include("pznahar/staticclass/clothing.php");
include(CORE_PATH."staticclass/magic.php");
#include("pznahar/staticclass/podkova.php");
include(CORE_PATH."staticclass/exchange.php");
include(CORE_PATH."staticclass/seed.php");
#include("pznahar/staticclass/food.php");
include(CORE_PATH."staticclass/good.php");
include(CORE_PATH."staticclass/present.php");
include(CORE_PATH."staticclass/tutorial.php");
include(CORE_PATH."staticclass/tree.php");


include(CORE_PATH."Session.php");
include(CORE_PATH."auther.php");
include(CORE_PATH."Auth.php");
include(CORE_PATH."user.php");
include(CORE_PATH."glade.php");
include(CORE_PATH."drier.php");
include(CORE_PATH."messages.php");
include(CORE_PATH."brute.php");
include(CORE_PATH."chance.php");
include(CORE_PATH."education.php");
include(CORE_PATH."paylog.php");
include(CORE_PATH."cvest.php");
include(CORE_PATH."draw.php");
include(CORE_PATH."wall.php");
include(CORE_PATH."help.php");


include(CORE_PATH."mantra/mantra.php");

include(CORE_PATH."storage/ingredient.php");
include(CORE_PATH."storage/elixir.php");
include(CORE_PATH."storage/storage.php");
#include("pznahar/storage/mortar.php");
#include("pznahar/storage/boiler.php");
#include("pznahar/storage/clothing.php");
#include("pznahar/storage/artifact.php");
#include("pznahar/storage/podkova.php");
include(CORE_PATH."storage/seed.php");
#include("pznahar/storage/food.php");
include(CORE_PATH."storage/good.php");

include(CORE_PATH."SecureRequest.class.php");
include(CORE_PATH."Kontakt.class.php");
include(CORE_PATH."deb.php");

include(CORE_PATH."statistic.php");

include(CORE_PATH."nottification/Nottification.class.php");

DBConnect::$useMemCache = false;




class pznahar extends Connect{
    
    private $stat;
        
    public function beforeFilter( $calledFunc ){        
        Session::start( );
        return true;        
        /*if($_SESSION['auth']){
            return true;                        
        }                        
        return in_array( $calledFunc, array('auth') );  */
    }
    
    
    
    #$this->logfile = "pznahar/logs/log.log";
    
    //{"0":{"api_url":"http:\/\/api.vkontakte.ru\/api.php","api_id":"1961804","api_settings":"1","viewer_id":"5357114","viewer_type":"0","sid":"b150aa3b6d511db183d237c0317dc851868d8a8a41ae0ce173ebbdab86","secret":"ba22d4a8d5","user_id":"0","group_id":"0","is_app_user":"1","auth_key":"4be98e7fe41da08591f952ecfefafae1","language":"0","parent_language":"0","lc_name":"882d891e"}}
    
    //{"api_url":"http:\/\/api.vkontakte.ru\/api.php","api_id":1961804,"api_settings":3,"viewer_id":14921442,"viewer_type":2,"sid":"27a2fd1b443c6da72a8cab0c493b2ed4f9b566513023dabca92963d289","secret":"b9ebc81b03","user_id":14921442,"group_id":0,"is_app_user":1,"auth_key":"045ce824fd9c4e7b8a4c1bed804df803","language":0,"parent_language":0,"lc_name":"b93d117b"}
    
    //{"api_id":1961804,"viewer_id":5357114,"sid":"9a0cf6f539b4ec5210a74fc1a316fc7ed82fac9f0bcd8ff1a985a29864","is_app_user":1,"auth_key":"4be98e7fe41da08591f952ecfefafae1"}
    /**
     * если новый юзер mess = "new user "success = true
     * если старый "success = true
     * [{"api_url":"http:\/\/api.vkontakte.ru\/api.php","api_id":"1961804","api_settings":"1","viewer_id":"5357114","viewer_type":"0","sid":"b150aa3b6d511db183d237c0317dc851868d8a8a41ae0ce173ebbdab86","secret":"ba22d4a8d5","user_id":"0","group_id":"0","is_app_user":"1","auth_key":"4be98e7fe41da08591f952ecfefafae1","language":"0","parent_language":"0","lc_name":"882d891e"}]
     * 
     * [{"api_url":"http:\/\/api.vkontakte.ru\/api.php","api_id":1961804,"api_settings":3,"viewer_id":14921442,"viewer_type":2,"sid":"26390f9b9119faae130112f557571f57b46943b1d2293c21ac0c653ab1","secret":"8afc32436c","user_id":14921442,"group_id":0,"is_app_user":1,"auth_key":"045ce824fd9c4e7b8a4c1bed804df803","language":0,"parent_language":0,"lc_name":"3b1cc744"}]
     * 
     * [{"api_url":"http:\/\/api.vkontakte.ru\/api.php","api_id":1961804,"api_settings":3,"viewer_id":18557049,"viewer_type":2,"sid":"9f177374144fa423b48574efa6e0d7820b66ed384e3d7399acedfbaea6","secret":"f509f44b69","user_id":14921442,"group_id":0,"is_app_user":1,"auth_key":"3b2b30b3dd04845a7951b818bf220131","language":0,"parent_language":0,"lc_name":"3b1cc744"}]
    */
    function auth($flashVars){
        $this->logStart("auth");
        #return $flashVars; 
        #return Auther::authorized($flashVars[0]);                                       
        $auth = new Auth($flashVars[0]);
        #return $auth; 
        $auth->_auth();
        $h = 1;
        if($auth->success == true){
            $u = new User();
            $u->getUser($auth->flashVars['viewer_id'], array(), true );
            $u->education($auth->flashVars['viewer_id']);
            $u->bonus($auth->flashVars['viewer_id']);
            $h = intval($u->user['help']);
            $e = intval($u->user['edu']);
        }else{
            $h = 0;
            $h = 0;
        }
        $this->logEnd();        
        return array("success" => $auth->success,
                    "mess" => $auth->message,
                    "error" => $auth->error,
                    "help" => $h,
                    "edu" => $e
                    ); 
    }
    
    /**
     * [flashVars, data[sex, skin, год рождения]]
     * [{"flashVars":{"api_url":"http:\/\/api.vkontakte.ru\/api.php","api_id":"1961804","api_settings":"1","viewer_id":"5357114","viewer_type":"0","sid":"b150aa3b6d511db183d237c0317dc851868d8a8a41ae0ce173ebbdab86","secret":"ba22d4a8d5","user_id":"0","group_id":"0","is_app_user":"1","auth_key":"4be98e7fe41da08591f952ecfefafae1","language":"0","parent_language":"0","lc_name":"882d891e"}, "data":{"bdate": "7.7.1987", "data":1, "skin":1, "yearCreate":1982}}]
     * 
     * [{"flashVars":{"api_url":"http:\/\/api.vkontakte.ru\/api.php","api_id":1961804,"api_settings":3,"viewer_id":14921442,"viewer_type":2,"sid":"26390f9b9119faae130112f557571f57b46943b1d2293c21ac0c653ab1","secret":"8afc32436c","user_id":14921442,"group_id":0,"is_app_user":1,"auth_key":"045ce824fd9c4e7b8a4c1bed804df803","language":0,"parent_language":0,"lc_name":"3b1cc744"}, "data":{"bdate": "7.7.1987", "data":1, "skin":1, "yearCreate":1982}}]
     * 
    */
    function registration($data){
        #htmlspecialchars_decode()
        #return $data[0]['flashVars'];
        #return Auther::authorized($data[0]['flashVars']);
        #return getdate( strtotime($data[0]['data']['bdate']) );
        $this->logStart("registration");
        
        $auth = new Auth($data[0]['flashVars']);
        if( !$auth->newUSer($data[0]['data']) ){
            return $this->error($auth->error);    
        }        
        $user = new User();
        if( intval($auth->flashVars['user_id']) != 0  ){
            $user->referUser(intval($auth->flashVars['user_id']));    
        }        
        #return 
        $user->first($auth->flashVars['viewer_id']);
        #return $this->answer($auth->locations->getLocations());
        return $this->answer(array("help" => $user->user['help'],
                                    "edu" => 0));
        
        /*
        return array("success" => $auth->success,
                    "mess" => $auth->message,
                    "error" => $auth->error);
        */ 
    }
    
    /**
     * [5357114]
     * [14921442]
    */
    function delMe($data){        
        $user = DBConnect::checkData($data[0]);
        $this->logStart("delMe");
        if(Auther::checkAuth($user) != true){
            return Connect::error("not auth");
        }
        
        $u = new User();
        #return $user;
        #return $this->answer($u->delAll());
        return $this->answer($u->delMe($user));
    }
    
    /**
     * tutorial
    */
    function getTutorials(){
        $this->logStart( "getTutorials " );
        $t = new Tutorial();
        return $this->answer( array('t' => $t->getTutorials(), 'type' => Tutorial::$type ) );
    }
    
    /**
     * пройти туторы
     * [ userId, (int)type ]
    */
    function setChangedTutorial($data){
        $data = DBConnect::checkData($data);
        $this->logStart("setChangedTutorial ,user: ".$data[0] );
        $u = new User();
        return $this->answer( $u->setChangeTutorial($data[0], $data[1]) );                
    }
    
    
    /**
     * [5357114]
    */
    function helpDone($data){
        $user = DBConnect::checkData($data[0]);
        $this->logStart("helpDone ,user: ".$user);
        if(Auther::checkAuth($user) != true){
            return Connect::error("not auth");
        }

        $u = new User();
        return $this->answer($u->doneHelp($user));
    }
    /**
     * [5357114]
    */
    function eduDone($data){
        $user = DBConnect::checkData($data[0]);
        $this->logStart("helpDone ,user: ".$user);
        if(Auther::checkAuth($user) != true){
            return Connect::error("not auth");
        }

        $u = new User();
        return $this->answer($u->doneEdu($user));
    }
    
    /**
     * [5357114, "showRosha"]
    */
    function goToL($data){
        $data = DBConnect::checkData($data);
        $this->logStart("goToL ,user: ".$data[0]);
        if(Auther::checkAuth($data[0]) != true){
            return Connect::error("not auth");
        }
        $l = new Help();
        return $l->goToL($data[1], $data[0]);        
    }
    
    /**
     * [5357114]
     * [14921442]
     * Инфа о скинах
     * 
    */
    function getSkins($kontaktid){
        $this->logStart("getSkins ,user: ".$kontaktid[0]);
        if(Auther::checkAuth($kontaktid[0]) != true){
            return Connect::error("not auth");
        }
        $kontaktid = DBConnect::checkData($kontaktid[0]);
        $user = new User(); 
        return $this->answer($user->getSkins($kontaktid));              
    }
    
    /**
     * [5357114]
     * [14921442]
     * Инфа о юзре
     * 
    */
    function getUser($kontaktid){
        $this->logStart("getUser ,user: ".$kontaktid[0]);
        if(Auther::checkAuth($kontaktid[0]) != true){
            return Connect::error("not auth");
        }
        $kontaktid = DBConnect::checkData($kontaktid[0]);
        $user = new User(); 
        return $this->answer($user->getUser($kontaktid));              
    }
    
    /**
     * [50]
    */
    function getWallPost($data){         
        $this->logStart("getWallPost ");
        $postId = DBConnect::checkData($data[0]);
        $u = new User();
        return $this->answer( $u->getWallPost($postId) );
    }
    
    /**
     * [5357114]
     * [14921442]
     * Заклинания
     * 
    */
    function getMyMantras($kontaktid){
        #return $kontaktid;
        $this->logStart("getMyMantras ,user: ".$kontaktid[0]);
        if(Auther::checkAuth($kontaktid[0]) != true){
            return Connect::error("not auth");
        }
        $kontaktid = DBConnect::checkData($kontaktid[0]);
        $user = new User(); 
        return $this->answer($user->getMyMantras($kontaktid));              
    }
    
    function getMagicInfo(){
        $this->logStart("getMagicInfo");
        Magic::getAll();
        return $this->answer(  Magic::$all  );
    }
    
    /**
     * [[5357114, 5357114]]
     * [14921442]
     * Инфа о игроках
     * 
    */
    function getInfoUsers($data){
        $data = DBConnect::checkData($data[0]);
        #return $data;
        $this->logStart("getInfoUsers ,user: ".$data[0]);
        if(Auther::checkAuth($data[0]) != true){
            return Connect::error("not auth");
        }
        
        $user = new User(); 
        return $this->answer($user->getInfoUsers($data[0], $data), false);              
    }
    
    /**
     * [5357114]
     * [14921442]
     * Обучение
     * 
    */
    function educatuon($kontaktid){
        $kontaktid = DBConnect::checkData($kontaktid[0]);
        $this->logStart("educatuon ,user: ".$kontaktid);
        if(Auther::checkAuth($kontaktid) != true){
            return Connect::error("not auth");
        }
        $user = new User(); 
        return $this->answer($user->education($kontaktid));              
    }
    
    /**
     * [5357114, 4]
     * Обучение за деньги
     * 
    */
    function buyEducatuon($data){
        $data = DBConnect::checkData($data);
        $this->logStart("buyEducatuon ,user: ".$data[0]);
        if(Auther::checkAuth($data[0]) != true){
            return Connect::error("not auth");
        }
        $user = new User(); 
        return $this->answer($user->buyEducation($data[0], $data[1]));              
    }
    
    /**
     * [5357114]
     * Изученые рецепты
     * 
    */
    function getEduRecipies($data){
        $u = DBConnect::checkData($data[0]);
        $this->logStart("buyEducatuon ,user: ".$u);
        if(Auther::checkAuth($u) != true){
            return Connect::error("not auth");
        }
        $user = new User(); 
        return $this->answer($user->getEduRecipies($u));              
    }
    
    
    /**
     * [5357114]
     * [14921442]
     * Бонус
     * 
    */
    function getBonus($kontaktid){
        $kontaktid = DBConnect::checkData($kontaktid[0]);
        $this->logStart("getBonus ,user: ".$kontaktid);
        if(Auther::checkAuth($kontaktid) != true){
            return Connect::error("not auth");
        }
        $user = new User(); 
        return $this->answer($user->bonus($kontaktid));              
    }
    
    /**
     * [5357114, "e0cbc4ba4f0ff6abd53b6f49b3b3d7e61223c926ec2e0055c5220dbbc42f9445"]
     * 
    */
    function setPeer($data){
        $data = DBConnect::checkData($data);
        $this->logStart("setPeer ,user: ".$data[0]);
        if(Auther::checkAuth($data[0]) != true){
            return Connect::error("not auth");
        }
        $user = new User(); 
        return $this->answer($user->setPeer($data[0], $data[1]));   
    }
    
    /**
     * [5357114]
     * [14921442]
     * Ступка, котел, подкова
     * 
    */
    function getAtributes($kontaktid){
        $kontaktid = DBConnect::checkData($kontaktid[0]);
        $this->logStart("getAtributes ,user: ".$kontaktid);
        if(Auther::checkAuth($kontaktid) != true){
            return Connect::error("not auth");
        }
        $user = new User(); 
        return $this->answer($user->getAtributes($kontaktid));  
    }
    
    /**
     * [5357114]
     * Возвращает то, что в сушилке
    */
    function getDrier($data){
        $this->logStart("getDrier ,user: ".$data[0]);
        if(Auther::checkAuth($data[0]) != true){
            return Connect::error("not auth");
        }
        $kontaktid = DBConnect::checkData($data[0]);
        $user = new User();
        return $this->answer($user->getDrier($kontaktid));
    }
    
    /**
     * [5357114, 0]
     * dotid  - точка роста 
     * Кладем в сушку, что нашли
    */
    function addToDrier($data){        
        $this->logStart("addToDrier ,user: ".$data[0]);
        if(Auther::checkAuth($data[0]) != true){
            return Connect::error("not auth");
        }
        $data = DBConnect::checkData($data);
        $user = new User();
        #return $user->addToDrier($data['viewer_id'], $data['dotid']);
        return $this->answer($user->addToDrier($data[0], $data[1]));
    }
    
    /**
     * [5357114]
     * dotid  - точка роста 
     * Возвращаем ингридиенты
    */
    function getIngridients($data){        
        $this->logStart("getIngridients ,user: ".$data[0]);
        #return $this->getMyStorage(array($data[0], 'ingredient'));  
        return $this->answer($this->getMyStorage(array($data[0], 'ingredient')));                      
    }
    
    /**
     * [5357114]
     * Возвращает то, что на поляне
    */
    function getGlade($kontaktid){
        $this->logStart("getGlade ,user: ".$kontaktid[0]);
        if(Auther::checkAuth($kontaktid[0]) != true){
            return Connect::error("not auth");
        }
        
        $kontaktid = DBConnect::checkData($kontaktid[0]);
        $user = new User();
        #return $user->getGlade($kontaktid);
        return $this->answer($user->getGlade($kontaktid));
    }
    
    /**
     * [5357114]
     * [14921442]
     * Возвращает зверюгу
    */
    function getPetInfo($kontaktid){        
        $this->logStart("getPetInfo ,user: ".$kontaktid[0]);
        if(Auther::checkAuth($kontaktid[0]) != true){
            return Connect::error("not auth");
        }
        $kontaktid = DBConnect::checkData($kontaktid[0]);
        $pet = new Brute();
        $pet->getPet($kontaktid);
        return $this->answer($pet->pet);        
    }
    
    /**
     * [5357114]
     * [14921442]
     * Возвращает зверюг
    */
    function getPet(){
        $this->logStart("getPets ");
        $p = array_values(Pet::getPets());
        $p[0]['skins'] = array_values($p[0]['skins']);
        return $this->answer($p[0]);
    }
    
    /**
     * [5357114, 7]
     * Кормит зверя
    */
    function foodPet($data){
        $data = DBConnect::checkData($data);
        $this->logStart("foodPet ,user: ".$data[0]);
        if(Auther::checkAuth($data[0]) != true){
            return Connect::error("not auth");
        }        
        $pet = new Brute();
        #return $pet->food($data[0], $data[1]);
        return $this->answer($pet->food($data[0], $data[1]));
    }
    /**
     * [5357114]
     * Возвращает родовое дерево
    */
    function getFamilyTree($kontaktid){
        $this->logStart("getFamilyTree ,user: ".$kontaktid[0]);
        if(Auther::checkAuth($kontaktid[0]) != true){
            return Connect::error("not auth");
        }
        $kontaktid = DBConnect::checkData($kontaktid[0]);
        $user = new User();        
        $this->logEnd();
        #return $this->complete($user->getTree($kontaktid));
        return $this->answer($user->getTree($kontaktid));
    }
    
    /**
     * [5357114, 1]
     * Поваренная книга [kontaktid, глава книги]
    */
    function getBook($data){
        $this->logStart("getBook ,user: ".$data[0]);
        if(Auther::checkAuth($data[0]) != true){
            return Connect::error("not auth");
        }
        $data = DBConnect::checkData($data);
        $user = new User();
        $this->logEnd();    
        #return $this->complete($user->getBook($data[1]));
        return $this->answer($user->getBook($data[1]));   
    }     
    
    /**
     * [5357114, 6]
     * Сварить зелье и докупить
    */
    function brewBuyGreen($data){
        $this->logStart("brewBuyGreen ,user: ".$data[0]);
        #return $this->brewGreen(array($data[0], $data[1], true) );
        #return $this->complete($this->brewGreen(array($data[0], $data[1], true) ));
        return $this->brewGreen(array($data[0], $data[1], true) );
    }
    
    /**
     * [5357114, 6]
     * Сварить зелье [kontaktid, $idgreen, докупить ]
    */
    function brewGreen($data){
        $this->logStart("brewGreen ,user: ".$data[0]);
        if(Auther::checkAuth($data[0]) != true){
            return Connect::error("not auth");
        }
        $data = DBConnect::checkData($data);
        $user = new User(); 
        #return $user->brewGreen($data[0], $data[1]);
        return $this->answer($user->brewGreen($data[0], $data[1], @$data[2]));
    }
    
    /**
     * Продать хабар
     * [5357114, {"11":1, "2":1},1]
     * [юзер, елементы хабара = array('element' => 'count'), валюта(1/2)]
     * 
    */
    function sellHabar($data){
        
        if(Auther::checkAuth($data[0]) != true){
            return Connect::error("not auth");
        }
        $data = DBConnect::checkData($data);
        $user = new User(); 
        return $this->answer($user->sellHabars($data[0], $data[1], $data[2]));
    }
    
    /**
     * Купить хабары
     * [5357114, {"11":1, "2":1},1]
     * [юзер, елементы хабара = array('element' => 'count'), валюта(1/2)]
     * 
     * 
    */
    function buyHabar($data){
        if(Auther::checkAuth($data[0]) != true){
            return Connect::error("not auth");
        }
        $data = DBConnect::checkData($data);
        $user = new User(); 
        return $this->answer($user->buyHabars($data[0], $data[1], $data[2]));
    }
    
    /**
     * Обмен денег
     * [user]
     * [5357114, 1, 10 ]
     * валюта, сколько обменять
    */
    function exchange($data){
        $this->logStart("exchange ,user: ".$data[0]);
        if(Auther::checkAuth($data[0]) != true){
            return Connect::error("not auth");
        }
        $data = DBConnect::checkData($data);
        $user = new User(); 
        return $this->answer($user->exchange($data[0], 1, $data[1] ));
    }
    
    /**
     * Курс обмена
     * [user]
     * [5357114]
     * 
    */
    function getCurs($data){
        $this->logStart("getCurs ,user: ".$data[0]);
        if(Auther::checkAuth($data[0]) != true){
            return Connect::error("not auth");
        }
        $data = DBConnect::checkData($data);
        #$user = new User();
        $this->logEnd();
        #return $this->complete(CURS);
        return $this->answer(CURS);
    }
    
    
    
    /**
     * Обмен денег
     * [user]
     * [5357114]
     * [14921442]
     * [18557049]
    */
    function getExchange($data){
        $data = DBConnect::checkData($data);
        $this->logStart("getExchange ,user: ".$data[0]);
        if(Auther::checkAuth($data[0]) != true){
            return Connect::error("not auth");
        }        
        $user = new User(); 
        #return $user->getExchange($data[0]);
        return $this->answer($user->getExchange($data[0]));
    }
    
    
    /**
     * Перевод денег
     * [user, golos, валюта]
     * [[5357114, 0, 1]]
     * [[14921442, 0, 1]]
    */
    function moveMoney($data){
        $data = DBConnect::checkData($data[0]);
        $this->logStart("moveMoney ,user: ".$data[0]);
        if(Auther::checkAuth($data[0]) != true){
            return Connect::error("not auth");
        }        
        $user = new User();
        if(!$data[2]) $data[2] = 1;
        #return $user->moveVoices($data[0], $data[1], $data[2]);
        return $this->answer($user->moveVoices($data[0], $data[1], $data[2])); 
    }
    
    /**
     * Купить еликсиры
     * [5357114, {"5":1, "9":1},1]
     * [юзер, еликсиры = array('elixir' => 'count'), валюта(1/2)]
     * 
     * 
    */
    function buyElixirs($data){
        if(Auther::checkAuth($data[0]) != true){
            return Connect::error("not auth");
        }
        $data = DBConnect::checkData($data);
        $user = new User();
        return $this->answer($user->buyElixirs($data[0], $data[1], $data[2]));  
    }
    
    /**
     * магаз
     * [5357114]
     * [юзер]
     * 
     * 
    */
    function getBuy($data){
        $this->logStart("getBuy ,user: ".$data[0]);
        if(Auther::checkAuth($data[0]) != true){
            return Connect::error("not auth");
        }
        $data = DBConnect::checkData($data);
        $user = new User();
        return $this->answer($user->getBuy());
    }
    
    /**
     * купить что-то
     * [5357114, "mortar", 1, 2]
     * [5357114, "artifact", 1, 2]
     * id товара
     * 
     * 
    */
    function buySome($data){
        
        $data = DBConnect::checkData($data);
        $this->logStart("buySome ,user: ".$data[0]);
        if(Auther::checkAuth($data[0]) != true){
            return Connect::error("not auth");
        }
        #return $data;
        $user = new User();
        /*$this->stat = array();
        $this->stat['user'] = $data[0];
        $this->stat['action'] = "buy";
        $this->stat['good'] = $data[2];
        $this->stat['goodType'] = $data[1];
        $this->stat['currency'] = array();
        */
        return $this->answer($user->buySome($data[0], $data[1], $data[2], intval(abs($data[3])) ));  
    }
    
    /**
     * продать что-то
     * [[5357114, "mortar", 1, 1]]
     * [[5357114, "artifact", 1, 1]]
     * id товара, count 
     * 
     * 
    */
    function sellSome($data){
        $data = DBConnect::checkData($data[0]);        
        $this->logStart("sellSome ,user: ".$data[0]);
        if(Auther::checkAuth($data[0]) != true){
            return Connect::error("not auth");
        }        
        $user = new User(); 
        return $this->answer($user->sellSome($data[0], $data[1], $data[2], intval(abs($data[3])) ));
    }
    
    /**
     * Подарить что-то
     * [5357114, 14921442, "mortar", 1]
     * [5357114, 14921442, "artifact", 1]
     * от кого, кому 
     * 
     * 
    */
    function presentSome($data){
        $data = DBConnect::checkData($data);
        $this->logStart("presentSome ,user: ".$data[0]);
        if(Auther::checkAuth($data[0]) != true){
            return Connect::error("not auth");
        }
        $user = new User(); 
        return $this->answer($user->presentSome($data[0], $data[1], $data[2], $data[3]));
    }
    
    /**
     * Обменять ступку
     * [5357114, 1, 2, 1]
     * юзер, ступка, валюта
    */
    function changeMortar($data){
        $this->logStart("changeMortar ,user: ".$data[0]);
        return $this->sellSome(array($data[0], 'mortar', $data[1], $data[3]));
    }
    /**
     * Обменять котел
     * [5357114, 1, 2, 1]
     * юзер, мой котел, новый котел, валюта
    */
    function changeBoiler($data){
        $this->logStart("changeBoiler ,user: ".$data[0]);
        return $this->sellSome(array($data[0], 'mortar', $data[1], $data[3]));
    }            
    
    
    /**
     * Get семачка
     * [5357114]
     * id товар 
     * 
     * 
    */
    function getSeed($data){        
        $this->logStart("getSeed ,user: ".$data[0]);
        return $this->getMyStorage(array($data[0], 'seed'));
    }
    
    /**
     * Get хавка
     * [5357114]
     * id товар 
     * 
     * 
    */
    function getFood($data){        
        $this->logStart("getFood ,user: ".$data[0]);
        return $this->getMyStorage(array($data[0], 'food'));
    }
    
    /**
     * Что в Сундуке?
     * [5357114, "mortar"]
     * id товар 
     * 
     * 
    */
    function getMyStorage($data){
        $this->logStart("getMyStorage ,user: ".$data[0]);
        if(Auther::checkAuth($data[0]) != true){
            return Connect::error("not auth");
        }
        $data = DBConnect::checkData($data);
        $user = new User();
        return $this->answer($user->getMyStorage($data[0], $data[1], true)); 
    }
    
    /**
     * Моя одежда
     * [5357114]
     * id товар 
     * 
     * 
    */
    function getClothing($data){
        $this->logStart("getClothing ,user: ".$data[0]);
        return $this->getMyStorage(array($data[0], 'clothing'));
    }
    /**
     * Мои артифакты
     * [5357114]
     * id товар 
     * 
     * 
    */
    function getArtefact($data){
        $this->logStart("getArtefact ,user: ".$data[0]);
        return $this->getMyStorage(array($data[0], 'artifact'));
    }
    /**
     * Мои еликсиры
     * [5357114]
     * id товар 
     * 
     * 
    */
    function getPotion($data){
        $this->logStart("getPotion ,user: ".$data[0]);
        return $this->getMyStorage(array($data[0], 'potion'));
    }
    
    
    /**
     * Использовать заклинание
     * [юзер, на кого, type, id]     
     * [5357114, 14921442, "potion", 6]
    */
    function useSubject($data){
        $this->logStart("useSubject ,user: ".$data[0]);
        if(Auther::checkAuth($data[0]) != true){
            return Connect::error("not auth");
        }
        $data = DBConnect::checkData($data);
        $user = new User();
        return $this->answer($user->useSubject($data[0], $data[1], $data[2], $data[3]));  
    }
    
    /**
     * Снять заклинание/предмет
     * [юзер, на кого, type, id]     
     * [5357114, 14921442, "potion", 6]
    */
    function UnUseSubject($data){
        $this->logStart("UnUseSubject ,user: ".$data[0]);
        if(Auther::checkAuth($data[0]) != true){
            return Connect::error("not auth");
        }
        $data = DBConnect::checkData($data);
        $user = new User();
        return $this->answer($user->unUseSubject($data[0], $data[1], $data[2], $data[3]));   
    }
    
    /**
     * использовать зелье 
     * [5357114, 14921442, 6]
     * [5357114, 5357114, 10]
     * юзер, противник, эликсир
    */
    function usePotion($data){
        $this->logStart("usePotion ,user: ".$data[0]);
        return $this->useSubject(array($data[0], $data[1], 'potion', $data[2]));
    }
    
    /**
     * Посадить семачку 
     * [5357114, 1]
     * [5357114, 1]
    */
    function useSeed($data){
        $this->logStart("useSeed ,user: ".$data[0]);
        return $this->useSubject(array($data[0], $data[0], 'seed', $data[1]));        
    }
    
    
    /**
     * Накинуть шмотку
     * [юзер, id]     
     * [5357114, 1]
    */
    function getDressGarderob($data){
        $this->logStart("getDressGarderob ,user: ".$data[0]);
        return $this->useSubject(array($data[0], $data[0], 'clothing', $data[1]));
    }
    
    /**
     * Юзать артефакт
     * [юзер, id]     
     * [5357114, 1]
    */
    function getDressArtefact($data){
        $this->logStart("getDressArtefact ,user: ".$data[0]);
        return $this->useSubject(array($data[0], $data[0], 'artifact', $data[1]));
    }
    
    
    /**
     * Снять шмотку
     * [юзер, id]     
     * [5357114, 1]
    */
    function getRemoveGarderob($data){
        $this->logStart("getRemoveGarderob ,user: ".$data[0]);
        return $this->UnUseSubject(array($data[0], $data[0], 'clothing', $data[1]));        
    }
    
    /**
     * Анюзать артефакт
     * [юзер, id]     
     * [5357114, 1]
    */
    function getRemoveArtefact($data){
        $this->logStart("getRemoveArtefact ,user: ".$data[0]);
        return $this->UnUseSubject(array($data[0], $data[0], 'artifact', $data[1]));                
    }
    
    
    
    /**
     * События
     * [5357114, 10]     
    */
    function getEvents($data){
        $this->logStart("getEvents ,user: ".$data[0]);
        if(Auther::checkAuth($data[0]) != true){
            return Connect::error("not auth");
        }
        $data = DBConnect::checkData($data);
        $arr = Messages::getAll($data[0], true, $data[1]);
        foreach($arr as $key => $val){
            //unset($arr[$key]['id']);
            //unset($arr[$key]['from']);
            //unset($arr[$key]['time']);
            $arr[$key]['time'] = array( strftime("%d" , $arr[$key]['time']), 
                                        strftime("%m" , $arr[$key]['time']), 
                                        strftime("%Y" , $arr[$key]['time']),
                                        strftime("%H" , $arr[$key]['time']),
                                        strftime("%M" , $arr[$key]['time']),
                                        strftime("%S" , $arr[$key]['time']),);
            unset($arr[$key]['user']);
        }
        return $this->answer($arr);
        /*
        $data = DBConnect::checkData($data);
        $user = new User(); 
        if($data = $user->useSubject($data[0], $data[1], $data[2], $data[3])){
            return $this->complete($data);
        }else{
            return $this->error(Connect::$__error);
        } 
        */
    }
    
    /**
     * События
     * [5357114, 10]     
    */
    function delEvents($data){
        $this->logStart("delEvents ,user: ".$data[0]);
        if(Auther::checkAuth($data[0]) != true){
            return Connect::error("not auth");
        }
        $data = DBConnect::checkData($data);
        return $this->answer( Messages::del($data[0], $data[1]) );
    }
    
    /**
     * Получить список друзей в приложении
     * [5357114, [14921442, 18557049]]
     * [вор, друзья]
    */
    function getMyPreys($data){
        $this->logStart("getMayPreys  ,user: ".$data[0]);
        if(Auther::checkAuth($data[0]) != true){
            return Connect::error("not auth");
        }
        $data = DBConnect::checkData($data);
        #return $data;    
        $user = new User();
        return $this->answer($user->getFrends($data[0], $data[1]));                 
    }
    
    
    /**
     * Получить поляну жертвы
     * [5357114, 14921442]
     * [ворб жертва]
    */
    function getGladePrey($data){
        $this->logStart("getGladePrey  ,user: ".$data[0]);
        if(Auther::checkAuth($data[0]) != true){
            return Connect::error("not auth");
        }
        $data = DBConnect::checkData($data);
        #return $data;    
        $user = new User();        
        return $this->answer($user->getGlade($data[1]));
        #return $this->complete($user->getGlade($data[1]));
        
    }
    
    /**
     * Получить жертву
     * [5357114, 14921442]
     * [ворб жертва]
    */
    function getPrey($data){
        $this->logStart("getPrey 'Получить поляну жертвы' ,user: ".$data[0]);
        if(Auther::checkAuth($data[0]) != true){
            return Connect::error("not auth");
        }
        $data = DBConnect::checkData($data);
        #return $data;    
        $user = new User();
        $pet = new Brute(); 
        $pet->getPet($data[1]) ;
        $t = $user->getTree($data[1]);
        $t[0] = array();        
        #$_SESSION['user'] = array();
        $array = array('pet' => $pet->pet,
                'tree' => array_values($t),
                'user' => $user->getUser($data[1], array(), false, false),
                'skin' => $user->getSkins($data[1]));
        #$_SESSION['user'] = array();
        #$this->logEnd();
        #return $this->complete($array);
        return $this->answer($array);
        
    }
    
    /**
     * Воровство
     * [14921442, 5357114, 0]
     * [5357114, 14921442, 0]
     * [кто, у кого, точка роста]     
    */
    function stealHabar($data){
        $this->logStart("stealHabar ,user: ".$data[0]);
        if(Auther::checkAuth($data[0]) != true){
            return Connect::error("not auth");
        }
        $data = DBConnect::checkData($data);            
        $user = new User(); 
        return $this->answer($user->addToDrier($data[0], $data[2], $data[1]));   
    }
    
    /**
     * поймать вора 
    */
    function getAttack($data){
        $this->logStart("getAttack ,user: ".$data[0]);
        if(Auther::checkAuth($data[0]) != true){
            return Connect::error("not auth");
        }
        $data = DBConnect::checkData($data);            
        $user = new User(); 
        return $this->answer($user->getAttak($data[0], $data[1]));
    }
    
    /**
     * Квесты
     * [5357114]
    */
    function getCvest($data){
        $data = DBConnect::checkData($data);
        $this->logStart("getCvest ,user: ".$data[0]);
        if(Auther::checkAuth($data[0]) != true){
            return Connect::error("not auth");
        }            
        $user = new User(); 
        return $this->answer($user->getCvest($data[0]));
    }
    
    /**
     * Лотерея
     * [5357114]
    */
    function getDraw($data){
        $data = DBConnect::checkData($data);
        $this->logStart("getDraw ,user: ".$data[0]);
        if(Auther::checkAuth($data[0]) != true){
            return Connect::error("not auth");
        }            
        $user = new User(); 
        return $this->answer($user->getDraw($data[0]));
    }
    /**
     * Лотерея
     * [5357114, 1]
    */
    function draw($data){
        $data = DBConnect::checkData($data);
        $this->logStart("getDraw ,user: ".$data[0]);
        if(Auther::checkAuth($data[0]) != true){
            return Connect::error("not auth");
        }            
        $user = new User(); 
        return $this->answer($user->draw($data[0], $data[1]));
    }
    
    /**
     * Реклама
     * [14921442]
    */
    function adt($data){
        $u = DBConnect::checkData($data[0]);
        $this->logStart("adt ,user: ".$u);
        if(Auther::checkAuth($u) != true){
            return Connect::error("not auth");
        }            
        $user = new User(); 
        return $this->answer($user->adt($u));
    }
    
    /**
    * Подарки
    */
    function getMyPresent($data){
        $u = DBConnect::checkData($data[0]);
        $this->logStart("getMyPresent ,user: ".$u);
        if(Auther::checkAuth($u) != true){
            return Connect::error("not auth");
        }            
        $user = new User(); 
        return $this->answer($user->present($u));
    }
    
    
    /**
     * Послать сообщение
     * [uid, text]
    */
    function sendEventMessage($data){
        $data = DBConnect::checkData($data);
        $u = intval($data[0]);
        $t = $data[1];
        $l = intval($data[2]);
        $this->logStart("sendEventMessage ,user: ".$u);
        Messages::sendUnicMessage($u, $t);
        $user = new User();
        $user->setLevelTree($u, $l);
        return $this->answer(true);
    }
    
    /**
     * []
     * manras
    */
    function getMagic(){
        //$this->logStart("getMantras ");
        Magic::getAll();   
        //return $this->answer(array_values( Magic::$all ));
    }
    
    /**
     * []
     * habars
    */
    function getHabars(){
        $this->logStart("getHabars ,user: ".$data[0]);
        Habars::getHabars();   
        return $this->answer(array_values( Habars::$habars ) );
    }
    
    /**
     * []
     * habars
    */
    function getElements(){
        Habars::getHabars();
        return Habars::$elements;   
    }        
    
    /**
     * []
     * getChance
    */
    function getChance(){
        $this->logStart("getChance ");
        $arr = array('1'=>10, '2'=>20, '3'=>30, '4'=>15);
        $chance = new Chance();
        $this->logEnd();
        return $chance->get($arr);           
    }
    
    function getS(){
        $this->logStart("getS ");
        return $this->answer($_SESSION); 
    }
    
    function getSwfUrl(){
        return SWF_URL;
    }
    
    private function answer($data, $debug = true){
        if($debug) @$this->logEnd();
        if($data === false){            
            return $this->error(Connect::$__error);
        }else{
            return $this->complete($data);
        } 
    }
    
    

}
?>
